import Vue from 'vue';
import VueRouter from 'vue-router';
// import Home from '../views/Home.vue';
import ProjectList from '@/components/ProjectList.vue';
import ProjectDetail from '@/components/ProjectDetail.vue';
import axios from 'axios';

Vue.use(VueRouter);
const clientId = '6741371790a54679ed76dccb94b1a49bd8817c5b2db12a6603080612b4315c65';
const clientSecret = '3f147403036c399d1973eb9ab3044095be1481f22684a0dfd42f2f8522d5746b';
const routes = [
  {
    path: '/',
    name: 'ProjectList',
    component: ProjectList,
    beforeEnter: (to, from, next) => {
      // debugger;
      // console.log(window.location.search);
      if (to.query.token) {
        next();
        return;
      }
      const obj = {};
      const reg = /([^?&=]+)=([^?&=]+)/g;
      let res = reg.exec(window.location.search);
      while (res) {
        // eslint-disable-next-line prefer-destructuring
        obj[res[1]] = res[2];
        res = reg.exec(window.location.search);
      }
      if (!obj.code) {
        next(false);
        window.location.href = 'https://gitee.com/oauth/authorize?client_id='
          + `${clientId}`
          + `&redirect_uri=${window.location.origin}${window.location.pathname}&response_type=code`;
      } else {
        axios.post('https://gitee.com/oauth/token?grant_type=authorization_code'
          + `&code=${obj.code}&`
          + `client_id=${clientId}&`
          + `redirect_uri=${window.location.origin}${window.location.pathname}&`
          + `client_secret=${clientSecret}`).then((response) => {
          console.log(response);
          next({
            path: '/',
            query: {
              token: response.data.access_token,
            },
          });
        });
      }
    },
  },
  {
    path: '/ProjectDetail',
    name: 'ProjectDetail',
    component: ProjectDetail,
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
  },
];

const router = new VueRouter({
  routes,
});

export default router;
